package com.ruoyi.module.excellentCase.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.module.excellentCase.vo.ExcellentCaseVO;
import com.ruoyi.module.excellentCaseDetail.domain.ExcellentCaseDetail;
import com.ruoyi.module.excellentCaseDetail.service.IExcellentCaseDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.excellentCase.domain.ExcellentCase;
import com.ruoyi.module.excellentCase.service.IExcellentCaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优秀案例Controller
 * 
 * @author yqq
 * @date 2024-04-18
 */
@RestController
@RequestMapping("/api/excellentCase")
public class ExcellentCaseController extends BaseController
{
    @Autowired
    private IExcellentCaseService excellentCaseService;

    @Autowired
    private IExcellentCaseDetailService excellentCaseDetailService;

    /**
     * 查询优秀案例列表
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExcellentCase excellentCase)
    {
        startPage();
        List<ExcellentCase> list = excellentCaseService.selectExcellentCaseList(excellentCase);
        return getDataTable(list);
    }

    /**
     * 导出优秀案例列表
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:export')")
    @Log(title = "优秀案例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExcellentCase excellentCase)
    {
        List<ExcellentCase> list = excellentCaseService.selectExcellentCaseList(excellentCase);
        ExcelUtil<ExcellentCase> util = new ExcelUtil<ExcellentCase>(ExcellentCase.class);
        util.exportExcel(response, list, "优秀案例数据");
    }

    /**
     * 获取优秀案例详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:query')")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable("caseId") Long caseId)
    {
        ExcellentCase excellentCase = excellentCaseService.selectExcellentCaseByCaseId(caseId);
        if (Objects.isNull(excellentCase)){
            return AjaxResult.error("案例不存在");
        }
        ExcellentCaseVO excellentCaseVO = new ExcellentCaseVO();
        BeanUtils.copyProperties(excellentCase,excellentCaseVO);
        ExcellentCaseDetail excellentCaseDetail = new ExcellentCaseDetail();
        excellentCaseDetail.setdCaseId(caseId);
        List<ExcellentCaseDetail> excellentCaseDetails = excellentCaseDetailService.selectExcellentCaseDetailList(excellentCaseDetail);
        if (CollUtil.isNotEmpty(excellentCaseDetails)){
            excellentCaseVO.setCaseDetailList(excellentCaseDetails);
        }
        return success(excellentCaseVO);
    }

    /**
     * 新增优秀案例
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:add')")
    @Log(title = "优秀案例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ExcellentCaseVO excellentCaseVO)
    {
        return excellentCaseService.insertExcellentCaseAndDetail(excellentCaseVO);
    }

    /**
     * 修改优秀案例
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:edit')")
    @Log(title = "优秀案例", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody ExcellentCaseVO excellentCaseVO)
    {
        return excellentCaseService.updateExcellentCaseVO(excellentCaseVO);
    }

    /**
     * 删除优秀案例
     */
    @PreAuthorize("@ss.hasPermi('api:excellentCase:remove')")
    @Log(title = "优秀案例", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{caseId}")
    public AjaxResult remove(@PathVariable Long caseId)
    {
        return excellentCaseService.deleteAllExcellentCaseByCaseId(caseId);
    }
}
